﻿#region references
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

/* Project dependencies */
using IcaGames.Logging;
using IcaGames.Objects;
using IcaGames.Database;
#endregion

namespace IcaGames.Database.ObjectManagement
{
    public static class PublisherManagement
    {
        /*
         * Function to get a Publisher with the specified publisherId.
         * 
         * @param
         * publisherId: The id of the Publisher that has to be loaded.
         * pb: The Publisher that has to be loaded.
         * 
         * @returns
         * True if the Publisher has succesfully been loaded.
         * False if the Publisher has not succesfully been loaded.
         */
        public static bool GetPublisher(int publisherId, Publisher pb)
        {
            SqlCommand command = null;
            SqlDataReader dr = null;
            string commandText = "spGetPublisher";

            try
            {
                command = Connection.GetCommand(commandText);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@publisherId", publisherId);

                dr = Connection.GetSqlDataReader(command);
                while (dr.Read())
                {
                    pb = new Publisher(
                        Cast.CastToInteger(dr["MAKER_ID"]),
                        Cast.CastToString(dr["NAAM"])
                        );
                }
            }
            catch (Exception ex)
            {
                Log.CreateLog("Exception while getting Publisher", "PUBLISHERMANAGEMENT");
                Log.CreateLog(ex.Message, "PUBLISHERMANAGEMENT");
                return false;
            }
            finally
            {
                Connection.CloseCommand(command, dr);
            }

            return true;
        }

        /*
         * Function to get a list with publishers for a string array with search keywords.
         * 
         * @param
         * keywords: The list with search keywords.
         * 
         * @returns
         * A list containing the Publishers matching the keywords.
         */
        public static List<Publisher> GetPublishers(string keywords)
        {
            List<Publisher> publishers = new List<Publisher>();
            SqlCommand command = null;
            SqlDataReader dr = null;
            string commandText = "spGetAdvancedSearchResultsPublisher";
            bool pubExists = false;
            string[] finalSearch = keywords.Split(' ');

            try
            {
                command = Connection.GetCommand(commandText);
                command.CommandType = CommandType.StoredProcedure;

                foreach (string keyword in finalSearch)
                {
                    command.Parameters.Clear();
                    command.Parameters.AddWithValue("@name", keyword);
                    pubExists = false;

                    dr = Connection.GetSqlDataReader(command);
                    while (dr.Read())
                    {
                        Publisher p = new Publisher(
                            Cast.CastToInteger(dr["MAKER_ID"]),
                            Cast.CastToString(dr["NAAM"])
                            );

                        /* Check if the game allready exists in the list before adding it to the list */
                        foreach (Publisher pub in publishers)
                        {
                            if (p.PublisherId == pub.PublisherId) { pubExists = true; break; }
                        }
                        if (!pubExists) { publishers.Add(p); }
                    }

                    /* Check if the datareader is still opened and close it if the reader is still opened */
                    if (dr != null) { dr.Close(); }
                }
            }
            catch (Exception ex)
            {
                Log.CreateLog("Exception while getting advanced Publisher search results", "PUBLISHERMANAGEMENT");
                Log.CreateLog(ex.Message, "PUBLISHERMANAGEMENT");
            }
            finally
            {
                Connection.CloseCommand(command);
            }

            return publishers;
        }
    }
}
